﻿using System;
using System.Collections.Generic;
using System.Text;
using KPIS.DBM;
using System.Configuration;
using KPIS.GERP.GIMS.MODEL;
using System.Data;

namespace KPIS.GERP.GIMS.DAL
{
    public class D_Employee
    {
        DBManager dbManager = new DBManager(DataProvider.MySQL, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();
        public IList<M_Employee> GetList()
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_Employee> infoList = new List<M_Employee>();
            try
            {
                dbManager.Open();
                strSQL = " SELECT  emp_seq,concat(coalesce(first_name, ''), '  ', coalesce(last_name, '')) as emp_name,job_site_name "
                         +" FROM hrm_ps_employees emp "
                         +" LEFT JOIN hrm_mp_job_sites js ON emp.working_job_site_seq = js.job_site_seq "
                         +" WHERE emp.record_status <>'D'";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_Employee info = new M_Employee();
                        info.emp_seq = Convert.ToInt32(drc[i]["emp_seq"]);
                        info.emp_name = drc[i]["emp_name"].ToString();
                        info.job_site_name = drc[i]["job_site_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
        public M_Employee GetListFillBySeq(Nullable<Int32> emp_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_Employee info = new M_Employee();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@emp_seq", emp_seq);

                strSQL = " SELECT  emp_seq,concat(coalesce(ms.prefix_name),'',coalesce(emp.first_name, ''), '  ', coalesce(emp.last_name, '')) as emp_name "
                         + " ,job_site_name,job_site_seq "
                         +" FROM hrm_ps_employees emp "
                         +" LEFT JOIN hrm_mp_job_sites js ON emp.working_job_site_seq = js.job_site_seq "
                         +" LEFT JOIN mas_name_prefixes ms ON emp.name_prefix_seq = ms.name_prefix_seq "
                         +" WHERE emp.record_status <>'D'"
                         +" and emp_seq = @emp_seq ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.emp_seq = Convert.ToInt32(drc[0]["emp_seq"]);
                    info.emp_name = drc[0]["emp_name"].ToString();
                    info.job_site_seq = cn.ConvertNullInt(drc[0]["job_site_seq"].ToString());
                    info.job_site_name = drc[0]["job_site_name"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }
        public M_Employee GetListApproveFillBySeq(Nullable<Int32> emp_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_Employee info = new M_Employee();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@emp_seq", emp_seq);

                strSQL =  " SELECT emp.emp_seq, p.pos_seq, p.pos_name,pp.pos_name  as approve_position "
                         +" ,concat(coalesce(mp.prefix_name),'',coalesce(emp.first_name, ''), '  ', coalesce(emp.last_name, '')) as emp_name "
                         +" ,concat('คุณ','',coalesce(upemp.first_name, ''), '  ', coalesce(upemp.last_name, '')) as approve_name,upemp.emp_seq as app_seq "
                         +" FROM hrm_ps_employees emp "
                         +" LEFT JOIN  hrm_mp_positions p on emp.pos_seq = p.pos_seq "
                         +" LEFT JOIN  hrm_mp_positions pp on p.pos_working_upper_seq = pp.pos_seq "
                         +" LEFT JOIN hrm_ps_employees upemp on pp.pos_seq =upemp.pos_seq "
                         +" LEFT JOIN mas_name_prefixes mp on emp.name_prefix_seq = mp.name_prefix_seq "
                         +" WHERE p.record_status = 'U'  and emp.emp_seq = @emp_seq ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.emp_seq = Convert.ToInt32(drc[0]["emp_seq"]);
                    info.emp_name = drc[0]["emp_name"].ToString();
                    info.emp_position = drc[0]["pos_name"].ToString();
                    info.app_seq = cn.ConvertNullInt(drc[0]["app_seq"]);
                    info.app_name = drc[0]["approve_name"].ToString();
                    info.app_position = drc[0]["approve_position"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

    }
}
